package com.example.course.converter;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.course.bean.Courses;
import com.example.course.bean.StudentCourse;
import com.example.course.bean.Teacher;
import com.example.course.dto.CourseDTO;
import com.example.course.service.StudentCourseService;
import com.example.course.service.TeacherService;
import com.example.course.vo.CourseVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 课程实体与DTO/VO转换器
 *
 * @author Jasmine-drs
 * @date 2025/4/25 10:45
 */
@Component
public class CourseConverter {

	private final TeacherService teacherService;
	private final StudentCourseService studentCourseService;

	public CourseConverter(TeacherService teacherService, StudentCourseService studentCourseService) {
		this.teacherService = teacherService;
		this.studentCourseService = studentCourseService;
	}

	/**
	 * 将CourseDTO转换为Courses实体
	 *
	 * @param dto CourseDTO
	 * @return Courses
	 */
	public Courses toEntity(CourseDTO dto) {
		if (dto == null) {
			return null;
		}
		Courses entity = new Courses();
		BeanUtils.copyProperties(dto, entity);

		// 设置教师姓名
		Teacher teacher = teacherService.getById(dto.getTeacherId());
		if (teacher != null) {
			entity.setTeacherName(teacher.getName());
		}

		// 设置创建时间和更新时间
		Date now = new Date();
		if (entity.getCreateTime() == null) {
			entity.setCreateTime(now);
		}
		entity.setUpdateTime(now);

		return entity;
	}

	/**
	 * 将Courses实体转换为CourseVO
	 *
	 * @param entity Courses
	 * @return CourseVO
	 */
	public CourseVO toVO(Courses entity) {
		if (entity == null) {
			return null;
		}

		CourseVO vo = new CourseVO();
		BeanUtils.copyProperties(entity, vo);

		// 查询选课学生数量
		long studentCount = studentCourseService.count(
				new LambdaQueryWrapper<StudentCourse>()
						.eq(StudentCourse::getCourseId, entity.getId())
		);
		vo.setStudentCount((int) studentCount);

		return vo;
	}

	/**
	 * 将Courses实体列表转换为CourseVO列表
	 *
	 * @param entityList Courses列表
	 * @return CourseVO列表
	 */
	public List<CourseVO> toVOList(List<Courses> entityList) {
		if (entityList == null || entityList.isEmpty()) {
			return new ArrayList<>();
		}

		// 获取所有课程ID
		List<Integer> courseIds = entityList.stream()
				.map(Courses::getId)
				.collect(Collectors.toList());

		// 批量查询每个课程的学生数量
		Map<Integer, Integer> studentCountMap = new java.util.HashMap<>();
		for (Integer courseId : courseIds) {
			long count = studentCourseService.count(
					new LambdaQueryWrapper<StudentCourse>()
							.eq(StudentCourse::getCourseId, courseId)
			);
			studentCountMap.put(courseId, (int) count);
		}

		// 转换为VO
		return entityList.stream().map(entity -> {
			CourseVO vo = new CourseVO();
			BeanUtils.copyProperties(entity, vo);

			// 设置选课学生数量
			vo.setStudentCount(studentCountMap.getOrDefault(entity.getId(), 0));

			return vo;
		}).collect(Collectors.toList());
	}
} 